//
//  RHDocumentCenterVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/4/26.
//

#import "RHDocumentCenterVC.h"
#import "RHDocumentCell.h"
#import "RHBaseWebViewController.h"
#import "RHPdfModel.h"
#import "RHSearchView.h"

@interface RHDocumentCenterVC ()<UITableViewDelegate,UITableViewDataSource,QMUITextFieldDelegate>
@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArr;

@property (nonatomic, strong) RHSearchView *searchView;

@property (nonatomic, assign) RequestType requestType;
//当前页数
@property (nonatomic, assign) NSInteger currentPage;
//总页数
@property (nonatomic, assign) NSInteger totalPage;
//每页多少条数据
@property (nonatomic, assign) NSInteger numOfPage;
@end

@implementation RHDocumentCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.view.backgroundColor = CFTabViewBgColor;
    self.dataArr = [NSMutableArray array];
    self.currentPage = 1;
    self.numOfPage = 10;
    self.requestType = RequestTypeRefresh;
    [self setupUI];
    [self getPdfFile];
}

-(void)getPdfFile
{
    [QMUITips showLoadingInView:self.view];
    NSDictionary *para = @{
                           @"pageNum":@(self.currentPage),
                           @"pageSize":@(self.numOfPage),
                           @"fileName":self.searchView.searchTextField.text,
                           @"type":@(self.type)
    };
    [RHPdfModel startGetWithCompletionAPIKey:H_getPdfFile argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        
        if (resultModel.code == 200) {
            NSInteger total = [request.responseObject[@"total"] integerValue];
            self.totalPage = total%self.numOfPage? total/self.numOfPage+1 : total/self.numOfPage;
            
            NSArray *arr = resultModel.data;
            //下拉刷新
            if(self.requestType == RequestTypeRefresh){
                [self.tableView endHeaderRefresh];
                self.dataArr = arr.mutableCopy;
            }else{//上拉加载
                [self.tableView endFooterRefresh];
                [self.dataArr addObjectsFromArray:arr];
            }
            if(self.currentPage >= self.totalPage){
                [self.tableView endFooterRefreshCustomWithNoMoreData];
            }
         
           
            if(arr.count == 0){
                [self.tableView endFooterRefreshWithNoMoreData];
            }
        }
        
        [self.tableView reloadData];
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
    }];
}

-(void)setupUI{

    kWeakSelf;
    _searchView = [[RHSearchView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 55)];
    [self.view addSubview:_searchView];
    _searchView.delegate = self;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView registerClass:[RHDocumentCell class] forCellReuseIdentifier:@"RHDocumentCell"];
    [_tableView setBackgroundColor:UIColor.clearColor];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setSeparatorColor:UIColor.clearColor];
    _tableView.tableFooterView = [UIView new];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    
    [self.view addSubview:_tableView];
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    }
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(weakSelf.searchView.mas_bottom);
        make.bottom.mas_offset(kSafe_bottom>0?-kSafe_bottom:-15);
    }];
    
    _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"没有数据".rh_localized detailStr:@""];
    
    [_tableView addHeaderRefresh:^{
        [weakSelf.tableView.mj_footer resetNoMoreData];
        weakSelf.requestType = RequestTypeRefresh;
        weakSelf.currentPage = 1;
        [weakSelf getPdfFile];
    }];
   
    [_tableView addBackFooterRefresh:^{
        weakSelf.requestType = RequestTypeAdd;
        weakSelf.currentPage++;
        [weakSelf getPdfFile];
    }];
    
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.tableView.mj_footer resetNoMoreData];
    self.requestType = RequestTypeRefresh;
    self.currentPage = 1;
    [self getPdfFile];
    [self.searchView resignFirstResponder];
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    [self.tableView.mj_footer resetNoMoreData];
    self.requestType = RequestTypeRefresh;
    self.currentPage = 1;
 
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self getPdfFile];
        [self.searchView resignFirstResponder];
    });
    return YES;
}


#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RHDocumentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHDocumentCell"];
    RHPdfModel *pdfModel = self.dataArr[indexPath.row];
    cell.fileNameLab.text = pdfModel.fileName;
    cell.typeContentLab.text = pdfModel.productType;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RHBaseWebViewController *pdfVC = [[RHBaseWebViewController alloc]init];
    RHPdfModel *pdfModel = self.dataArr[indexPath.row];
    pdfVC.urlStr = pdfModel.fileLink;
    pdfVC.navTitle = pdfModel.fileName;
    [self.navigationController pushViewController:pdfVC animated:YES];
}


@end
